package com.sx.zongzhi.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.sx.zongzhi.BaseActivity;
import com.sx.zongzhi.R;
import com.sx.zongzhi.adapter.SpinnerPubAdapter;
import com.sx.zongzhi.bean.EducationBean;
import com.sx.zongzhi.bean.GlobalBean;
import com.sx.zongzhi.bean.MsgEvent;
import com.sx.zongzhi.bean.PopulationFixedSearchBean;
import com.sx.zongzhi.bean.PublicBean;
import com.sx.zongzhi.presenter.PublicListPest;
import com.sx.zongzhi.utils.BaseUtil;
import com.sx.zongzhi.utils.MyRxBus;
import com.sx.zongzhi.utils.ToastUtils;
import com.sx.zongzhi.view.IPublicBeanView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 留守人口查询
 */
public class FixedSearchActivity extends BaseActivity implements IPublicBeanView{

    @BindView(R.id.et_Name) EditText et_Name;
    @BindView(R.id.et_IDCardNum) EditText et_IDCardNum;
    @BindView(R.id.et_Address) EditText et_Address;
    @BindView(R.id.sp_Cancel) Spinner sp_Cancel;
    @BindView(R.id.sp_Death) Spinner sp_Death;
    @BindView(R.id.sp_Sex) Spinner sp_Sex;
    @BindView(R.id.sp_Nation) Spinner sp_Nation;
    @BindView(R.id.sp_Marriage) Spinner sp_Marriage;
    @BindView(R.id.sp_PersonType) Spinner sp_PersonType;
    @BindView(R.id.tv_Education) TextView tv_Education;
    @BindView(R.id.sp_Relation) Spinner sp_Relation;//与留守人员关系
    @BindView(R.id.sp_FixedType) Spinner sp_FixedType;//留守人员类型
    @BindView(R.id.tv_BirthStartTime) TextView tv_BirthStartTime;
    @BindView(R.id.tv_BirthEndTime) TextView tv_BirthEndTime;
    private EducationBean.SchoolingsBean schoolingsBean;


    @Override
    public void setUI(Bundle savedInstanceState) {
        setContentView(R.layout.activity_fixed_search);
        setTitleName("常住人口查询");
        setTitleRight(R.string.search);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {
        PublicListPest publicListPest=new PublicListPest(this);
        publicListPest.getPublicList("性别");
        publicListPest.getPublicList("民族");
        publicListPest.getPublicList("婚姻状况");
        publicListPest.getPublicList("与户主关系");
        publicListPest.getPublicList("留守人员类型");
        publicListPest.getPublicList("政治面貌");
    }

    @OnClick({R.id.tv_right,R.id.tv_Education,R.id.tv_BirthStartTime,R.id.tv_BirthEndTime})
    public void OnClick(View view){
        switch (view.getId()){
            case R.id.tv_right:
                submit();
                break;
            case R.id.tv_Education:
                startActivityForResult(new Intent(this,EducationActivity.class),4447);
                break;
            case R.id.tv_BirthStartTime:
                BaseUtil.createDataPop(this,tv_BirthStartTime);
                break;
            case R.id.tv_BirthEndTime:
                BaseUtil.createDataPop(this,tv_BirthEndTime);
                break;
        }
    }

    private void submit() {
        String name=et_Name.getText().toString().trim();
        String idCardNum=et_IDCardNum.getText().toString().trim();
        String address=et_Address.getText().toString().trim();
        int cancelState=sp_Cancel.getSelectedItemPosition()==0?0:1;
        int deathState=sp_Death.getSelectedItemPosition()==0?0:1;
        GlobalBean sexBean=(GlobalBean)sp_Sex.getSelectedItem();
        String sexId=(sexBean.getId()).equals("-1") ?"":sexBean.getId();
        GlobalBean nBean=(GlobalBean)sp_Nation.getSelectedItem();
        String nationId=(nBean.getId()).equals("-1") ?"":nBean.getId();
        GlobalBean mBean=(GlobalBean)sp_Marriage.getSelectedItem();
        String marriageId=(mBean.getId()).equals("-1") ?"":mBean.getId();
        GlobalBean perBean=(GlobalBean)sp_PersonType.getSelectedItem();
        String perTypeId=(perBean.getId()).equals("-1") ?"":perBean.getId();
        String schoolings="";
        if (schoolingsBean!=null){
            schoolings=schoolingsBean.getValue();
        }
        GlobalBean iRBean=(GlobalBean)sp_Relation.getSelectedItem();
        String rRelationId=(iRBean.getId()).equals("-1") ?"":iRBean.getId();
        GlobalBean fBean=(GlobalBean)sp_FixedType.getSelectedItem();
        String fixedId=(fBean.getId()).equals("-1") ?"":iRBean.getId();

        String regStartTime=tv_BirthStartTime.getText().toString();
        String regEndTime=tv_BirthEndTime.getText().toString();

        PopulationFixedSearchBean searchBean=new PopulationFixedSearchBean();
        searchBean.setName(name);
        searchBean.setIdCardNo(idCardNum);
        searchBean.setCurrentAddress(address);
        searchBean.setIsEmphasis(cancelState);
        searchBean.setIsDeath(deathState);
        searchBean.setGender(sexId);
        searchBean.setNation(nationId);
        searchBean.setMaritalState(marriageId);
        searchBean.setLeftType(fixedId);
        searchBean.setSchoolStr(schoolings);
        searchBean.setLeftRelationship(rRelationId);
        searchBean.setPoliticalBackground(perTypeId);
        searchBean.setBirthday(regStartTime);
        searchBean.setEndBirthday(regEndTime);
        MyRxBus.get().post(new MsgEvent<>("FixedActivity",1,searchBean));
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 4447://学历
                    schoolingsBean= ( EducationBean.SchoolingsBean)
                            data.getSerializableExtra("SchoolingsBean");
                    tv_Education.setText(schoolingsBean.getName());
                    break;
            }
        }
    }


    @Override
    public void showError(String message) {
        ToastUtils.showToast(this,message);
    }

    @Override
    public void getPublicList(PublicBean publicBean) {
        if ("性别".equals(publicBean.getDomainName())){
            publicBean.getDicts().add(0,new GlobalBean("未选择","-1"));
            setSpinnerAdapter(sp_Sex,publicBean.getDicts());
        }
        if ("民族".equals(publicBean.getDomainName())){
            publicBean.getDicts().add(0,new GlobalBean("未选择","-1"));
            setSpinnerAdapter(sp_Nation,publicBean.getDicts());
        }
        if ("婚姻状况".equals(publicBean.getDomainName())){
            publicBean.getDicts().add(0,new GlobalBean("未选择","-1"));
            setSpinnerAdapter(sp_Marriage,publicBean.getDicts());
        }
        if ("与户主关系".equals(publicBean.getDomainName())){
            publicBean.getDicts().add(0,new GlobalBean("未选择","-1"));
            setSpinnerAdapter(sp_Relation,publicBean.getDicts());
        }
        if ("留守人员类型".equals(publicBean.getDomainName())){
            publicBean.getDicts().add(0,new GlobalBean("未选择","-1"));
            setSpinnerAdapter(sp_FixedType,publicBean.getDicts());
        }
        if ("政治面貌".equals(publicBean.getDomainName())){
            publicBean.getDicts().add(0,new GlobalBean("未选择","-1"));
            setSpinnerAdapter(sp_PersonType,publicBean.getDicts());
        }
    }

    private void setSpinnerAdapter(Spinner spinner, List<GlobalBean> lists) {
        SpinnerPubAdapter adapter=new SpinnerPubAdapter(this,R.layout.item_text,lists);
        spinner.setAdapter(adapter);
    }
}
